<?php
/**
* 用户注册
* @author moufer<moufer@163.com>
* @copyright Moufer Studio(www.modoer.com)
*
*
*
*    ?act=user&op=reg&devid=XXXXXXXX 注册手机用户名
*    ?act=user&op=login&mid=XX&token=XXXXXX&code=XXXX 手机登录
*/
!defined('IN_MUDDER') && exit('Access Denied');
define('SCRIPTNAV', 'user');

// if($user->isLogin) redirect('member_reg_logined');

$forward = $_GET['forward'] ? $_GET['forward'] : $_G['cfg']['siteurl'];
if(strposex($forward,'op=logout') || !strposex($forward, $_G['web']['domain'])) {
    $forward = $_G['cfg']['siteurl'];
}
$forward = _T(rawurldecode(rawurldecode($forward)));

// $mobile = $_G['loader']->model('howcan:user');

$_G['loader']->helper('validate');

switch($_GET['op']) {
case 'check_username':
    if(!$username = trim($_POST['username'])) {
        echo lang('member_reg_ajax_name_empty'); exit;
    }
    if($_G['charset'] != 'utf-8') {
        $_G['loader']->lib('chinese', NULL, FALSE);
        $CHS = new ms_chinese('utf-8', $_G['charset']);
        $username = _T($CHS->Convert($username));
    }
    $user->check_username($username, true);
    if($user->check_username_exists($username)) {
        echo lang('member_reg_ajax_name_exists');
        exit;
    }
    echo lang('member_reg_ajax_name_normal'); exit;
    break;
case 'check_email':
    if(!$email = trim($_POST['email'])) {
        echo lang('member_reg_ajax_email_empty'); exit;
    }
    if(!validate::is_email($email)) {
        echo lang('member_reg_ajax_email_invalid'); exit;
    }
    if(!$MOD['existsemailreg'] && $user->check_email_exists($email)) {
        echo lang('member_reg_ajax_email_exists');
        exit;
    }
    echo lang('member_reg_ajax_email_normal'); exit;
    break;
case 'reg':
	// 自动注册手机用户
	if (isset($_GET['devid'])) {
		$devid = $_GET['devid'];
		if (!$mobile->check_devid_exists($devid)) {
			$uid = $mobile->register($mobile->get_post($_GET));
		} else {
			$mobile->dev_login($devid);
		}
	}
    break;

case 'mlogin':
    if($mobile->isLogin) errMessage(1,'用户已登录');
//	if($user->passport['enable']) location($user->passport['login_url']);
//    if(!$_POST['onsubmit']) location(url('member/login'));
//    if($MOD['seccode_login']) check_seccode($_POST['seccode']);
    if(!$sync = $mobile->login($_GET['mid'], $_GET['token'], $_GET['code'])) {
        errMessage(1,'登录失败');
    }
    redirect(lang('member_login_succeed') . $sync, get_forward(url('howcan/index'),1));
	break;

case 'ulogin':
//    if($user->isLogin) errMessage(2,'用户已登录');
	if($user->passport['enable']) location($user->passport['login_url']);
//    if(!$_POST['onsubmit']) location(url('howcan/login'));
//    if($MOD['seccode_login']) check_seccode($_POST['seccode']);
    if(!$sync = $user->login($_POST['username'], $_POST['password'], $_POST['life'])) {
		errMessage(2,'登录失败'); // redirect('member_login_lost');
    }
	
    errMessage(0,json_encode(array('uid'=>$user->uid,'nickname'=>$_POST['username']))) ; // redirect(lang('member_login_succeed') . $sync, get_forward(url('howcan/index'),1));
	break;	
case 'checkAccount':
    if($user->isLogin) errMessage(0,json_encode(array('uid'=>$user->uid,'nickname'=>$user->nickname))); else errMessage(1,'需要重新登录');
	break;
default:
    break;
}

// if ($mobile->isLogin)
echo json_encode(array('login'=>($mobile->isLogin)?1:0));

?>